﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace VBACodeingTool
{
    public static class MouseHelper
    {
        public static void GetCaretPosition(out int left, out int top)
        {
            string name = VBEHelper.GetCurrentCodePane().Window.Caption;
            IntPtr hwndParent = (IntPtr)ProgramInstanceManager.VBE.MainWindow.HWnd;
            IntPtr hwndSon = User32.FindWindowEx(hwndParent, 0, "MDIClient", null);
            IntPtr hwndCode = User32.FindWindowEx(hwndSon, 0, "VbaWindow", name);
            User32.GetWindowRect(VBEHelper.GetCodeWindowHwnd(), out User32.RECT rect);
            User32.GetWindowRect(hwndParent, out User32.RECT rectParent);
            User32.GUITHREADINFO threadInfo = new User32.GUITHREADINFO();
            threadInfo.cbSize = Marshal.SizeOf(threadInfo);
            User32.GetGUIThreadInfo(User32.GetCurrentThreadId(), ref threadInfo);
            //Debug.WriteLine($"X:{threadInfo.rcCaret.X},Y:{threadInfo.rcCaret.Y},Width:{threadInfo.rcCaret.Width},Height:{threadInfo.rcCaret.Height}");
            //Debug.WriteLine($"left:{rect.Left},top:{rect.Top},right:{rect.Right},bottom:{rect.Bottom}");
            left = threadInfo.rcCaret.X + rect.Left ;
            top = threadInfo.rcCaret.Height+ rect.Top + 30;
        }
        public static bool IsMouseInForm(IntPtr hwnd)
        {
            User32.GetWindowRect(hwnd,out User32.RECT lpRect);
            User32.GetCursorPos(out User32.MousePosition lpPoint);

            if (lpPoint.x > lpRect.Right)
            {
                return false;
            }
            if (lpPoint.y > lpRect.Bottom)
            {
                return false;
            }
            if (lpPoint.x < lpRect.Left)
            {
                return false;
            }
            if (lpPoint.y < lpRect.Top)
            {
                return false;
            }
            return true;
        }
    }
}
